package com.lsh.service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lsh.common.util.PageSurport;
import com.lsh.dao.TsysRightDao;
import com.lsh.model.TsysRight;

@Service("tsysRightService")
public class TsysRightService extends BasicService {

	private final static Log LOG = LogFactory.getLog(TsysRight.class);
	@Autowired
	TsysRightDao dao;

	/**
	 * 插入数据
	 * 
	 * @param tsysRight
	 * @return
	 */
	public int insert(TsysRight tsysRight) {
		return dao.insert("TsysRight-insert", tsysRight);
	}

	/**
	 * 根据ID获取记录
	 * 
	 * @param tsysRight
	 * @return
	 */
	public TsysRight getById(String no) {
		return dao.getById("TsysRight-getById", no);
	}

	/**
	 * 删除记录
	 * 
	 * @param object
	 * @return
	 */
	public int delete(TsysRight tsysRight) {
		return dao.delete("TsysRight-delete", tsysRight);
	}

	/**
	 * 更新记录
	 * 
	 * @param tsysUser
	 */
	public void update(TsysRight tsysRight) {
		dao.update("TsysRight-update", tsysRight);
	}

	/**
	 * 获取分页数据
	 * 
	 * @param map
	 * @param page
	 * @return
	 */
	public PageSurport getPage(Map map, PageSurport page) {
		return dao.selectPageByPage("TsysRight-getPage",
				"TsysRight-getPageCount", page, map);
	}

	/**
	 * 获取全部记录
	 * 
	 * @param orderBy
	 * @return
	 */
	public List getAllBy(Map map) {
		return dao.selectList("TsysRight-getAllBy", map);
	}

	/**
	 * 获取全部记录
	 * 
	 * @param orderBy
	 * @return
	 */
	public List<TsysRight> getAll() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("grade", 1);
		List<TsysRight> list = dao.selectList("TsysRight-getByMap", map);
		map.remove("grade");
		for (TsysRight tsysRight : list) {
			map.put("parentid", tsysRight.getRightid());
			List<TsysRight> list2 = dao.selectList("TsysRight-getByMap", map);
			tsysRight.setChildList(list2);
		}
		return list;
	}

	public Set<String> getRightsById(String roleId) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("roleid", roleId);
		List<String> list = dao.selectList("TsysRight-getRightsById", map);
		Set set = new HashSet<String>(list);
		return set;
	}

	private void digui(TsysRight right) {
		String parentid = right.getRightid();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentid", parentid);
		List<TsysRight> list = this.getAllBy(map);
		if (list.size() <= 0) {
			return;
		} else {
			right.setChildList(list);
			for (TsysRight tsysRight : list) {
				digui(tsysRight);
			}
		}
	}

	public TsysRight getTree(String parentid) {
		TsysRight right = this.getById("ROOT");
		digui(right);
		System.out.println(right.getChildList().size());
		TsysRight right2 = (TsysRight) right.getChildList().get(1);
		System.out.println(right2.getChildList().get(0).getChildList().get(0).getRightname());
		return right;
	}
}
